package com.inspur.cloud.service.dataspace;

import com.inspur.cloud.configuration.ApiConfig;
import com.inspur.cloud.entity.ambari.ClusterServicesEntity;
import com.inspur.cloud.entity.dataspace.UserEntity;
import com.inspur.cloud.entity.dataspace.ao.user.LoginAO;
import com.inspur.cloud.security.AuthorizationHelper;
import com.inspur.cloud.security.exception.LoginException;
import com.inspur.cloud.service.ambari.ClusterServicesSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LoginService {

    public static final String MESSAGE = "用户名或密码错误";

    public static final String CHANGCE = "用户名或密码不正确，本日还有%d次机会，否则账户会被锁定";

    public static final String NOCHANGCE = "用户名或密码不正确，本日已无机会，账户已被锁定";

    public final static Integer EXPIRE_DAY = 90;

    public final static Long ONE_DAY = 1000*60*60*24L;

    private final static String[] SERVICES = new String[]{"HDFS", "HIVE", "HBASE", "YARN", "KAFKA"};

    @Autowired
    private ApiConfig apiConfig;
    @Autowired
    private UserManagerService userManagerService;
    @Autowired
    private ClusterServicesSerivce clusterServicesSerivce;

    public LoginAO oper() {
        UserEntity user = AuthorizationHelper.getUser();
        LoginAO login = new LoginAO();
        login.setId(user.getId());
        login.setUsername(user.getName());
        login.setRole(user.getRole());
        boolean isAdmin = AuthorizationHelper.isAdmin(user);
        //管理员没有修改密码，要求强制修改密码,非管理员要求90天修改密码
        if(isAdmin) {
            login.setModifyDay(-1);
            if (user.getPasswordModifyTime() == null) {
                login.setModifyPass(true);
            } else {
                login.setModifyPass(false);
            }
        }else{
            Integer day = EXPIRE_DAY - 1 - new Long((new Date().getTime() - user.getPasswordModifyTime().getTime()) / ONE_DAY).intValue();
            login.setModifyDay(day);
        }
        List<String> serviceList = getServiceList();
        login.setResourceList(serviceList);

        return login;
    }

    public void checkLogin(UserEntity user) {
        if(user.getFailureTimes()>=apiConfig.getFailureTimes()){
            throw new LoginException("该账户已被锁定,请联系系统管理员");
        }
        if(!AuthorizationHelper.isAdmin(user)){
            Integer day = EXPIRE_DAY - 1 - new Long((new Date().getTime() - user.getPasswordModifyTime().getTime()) / ONE_DAY).intValue();
            if(day<0){
                throw new LoginException("用户密码信息已过期,请修改密码或联系系统管理员");
            }
        }
    }

    public String increaseFailureTimes(String username) {
        if(username==null){
            return MESSAGE;
        }
        UserEntity user = userManagerService.findByName(username);
        Integer failureTimes = user.getFailureTimes()+1;
        user.setFailureTimes(failureTimes);
        userManagerService.save(user);
        if (failureTimes>=apiConfig.getFailureTimes()){
            return NOCHANGCE;
        }else{
            return String.format(CHANGCE,apiConfig.getFailureTimes()-failureTimes);
        }
    }


    public List<String> getServiceList() {
        List<ClusterServicesEntity> serviceList = clusterServicesSerivce.findAll();
        List<String> result = new ArrayList<>();
        for (ClusterServicesEntity clusterServices : serviceList) {
            for (String service : SERVICES) {
                if (service.equalsIgnoreCase(clusterServices.getServiceName())) {
                    result.add(service);
                }
            }
        }
        return result;
    }
}
